package com.easyspringboot.bulider;

import com.easyspringboot.bean.Constants;
import com.easyspringboot.bean.FieldInfo;
import com.easyspringboot.bean.TableInfo;
import com.easyspringboot.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiaoyi
 * @Version 1.0
 * @ClassName BuildPo
 * @Time 21:33
 */
public class BuildQuery {
    private static final Logger logger = LoggerFactory.getLogger(BuildQuery.class);

    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_QUERY);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File poFile = new File(folder, tableInfo.getBeanName() + Constants.SUFFIX_BEAN_QUERY + ".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(poFile);
            outw = new OutputStreamWriter(out, "utf8");
            bw = new BufferedWriter(outw);
            bw.write("package " + Constants.PACKAGE_QUERY + ";");
            bw.newLine();
            bw.newLine();
//            logger.info("Date:{},DateTime:{}",tableInfo.getHavaDate(),tableInfo.getHaveDateTime());
            //判断是否有日期类型

            if (tableInfo.getHavaDate() || tableInfo.getHaveDateTime()) {
                bw.newLine();
                bw.write("import java.util.Date;");
                bw.newLine();

            }


            bw.newLine();
            if (tableInfo.getHaveBigDecimal()) {
                bw.write("import java.math.BigDecimal;");
                bw.newLine();
            }
            bw.newLine();
            bw.newLine();
            //创建类注释
            BuildComment.createClassComment(bw, tableInfo.getComment() + "查询条件");
            bw.write("public class " + tableInfo.getBeanName() + Constants.SUFFIX_BEAN_QUERY + " extends BaseQuery{");
            bw.newLine();
            bw.newLine();
            List<FieldInfo> extendList = new ArrayList<>();
            for (FieldInfo fied : tableInfo.getFieldList()) {
                BuildComment.createFieldComment(bw, fied.getComment());//创建属性注释
                bw.write("\tprivate  " + fied.getJavaType() + " " + fied.getPropertyName() + ";");
                bw.newLine();
                bw.newLine();
                //模糊查询
                //判断是否是字符串类型
                if (ArrayUtils.contains(Constants.SQL_STRING_TYPES, fied.getSqlType())) {
                    bw.write("\tprivate   " + fied.getJavaType() + " " + fied.getPropertyName() + Constants.SUFFIX_BEAN_QUERY_FUZZY + ";");
                    bw.newLine();
                    bw.newLine();

                    FieldInfo fuzzyField = new FieldInfo();
                    fuzzyField.setJavaType(fied.getJavaType());
                    fuzzyField.setPropertyName(fied.getPropertyName() + Constants.SUFFIX_BEAN_QUERY_FUZZY);
                    extendList.add(fuzzyField);
                }
                if (ArrayUtils.contains(Constants.SQL_DATE_TYPES, fied.getSqlType()) || ArrayUtils.contains(Constants.SQL_DATA_TIME_TYPES, fied.getSqlType())) {
                    //时间范围查询
                    bw.write("\tprivate   String" + " " + fied.getPropertyName() + Constants.SUFFIX_BEAN_QUERY_TIME_START + ";");
                    bw.newLine();
                    bw.newLine();
                    bw.write("\tprivate   String" + " " + fied.getPropertyName() + Constants.SUFFIX_BEAN_QUERY_TIME_END + ";");
                    bw.newLine();
                    bw.newLine();
                    FieldInfo startField = new FieldInfo();
                    startField.setJavaType("String");
                    startField.setPropertyName(fied.getPropertyName() + Constants.SUFFIX_BEAN_QUERY_TIME_START);
                    extendList.add(startField);
                    FieldInfo endField = new FieldInfo();
                    endField.setJavaType("String");
                    endField.setPropertyName(fied.getPropertyName() + Constants.SUFFIX_BEAN_QUERY_TIME_END);
                    extendList.add(endField);
                }

            }
            List<FieldInfo> fieldList = tableInfo.getFieldList();

            //get,set方法
            bulidCreateGetSet(bw, fieldList);
            bulidCreateGetSet(bw, extendList);
            StringBuffer toString = new StringBuffer();
            for (FieldInfo fied : tableInfo.getFieldList()) {
                String paramName = fied.getPropertyName();
                //判断是否有日期类型
                if (ArrayUtils.contains(Constants.SQL_DATA_TIME_TYPES, fied.getSqlType())) {
                    paramName = "DateUtils.format(" + paramName + "," + "DateTimeEnum.YYYY_MM_DD_HH_MM_SS.getPattern()" + ")";
                } else if (ArrayUtils.contains(Constants.SQL_DATE_TYPES, fied.getSqlType())) {
                    paramName = "DateUtils.format(" + paramName + "," + "DateTimeEnum.YYYY_MM_DD.getPattern()" + ")";
                }
                toString.append("\"" + fied.getComment() + " : \" + (" + paramName + " == null ? \"空\" : " + paramName + ") + \",\" + ");
            }


            bw.write("}");
            bw.newLine();


        } catch (Exception e) {
            logger.error("创建文件失败", e);
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }


    //get,set方法
    private static void bulidCreateGetSet(BufferedWriter bw, List<FieldInfo> fieldList) throws IOException {
        for (FieldInfo fied : fieldList) {
            String tempField = StringUtils.uperCaseFirstLetter(fied.getPropertyName());//boolean类型需要特殊处理
            bw.write("\tpublic void set" + tempField + "(" + fied.getJavaType() + " " + fied.getPropertyName() + ") {");
            bw.newLine();
            bw.write("\t\tthis." + fied.getPropertyName() + " = " + fied.getPropertyName() + ";");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
            bw.newLine();
            bw.write("\tpublic " + fied.getJavaType() + " get" + tempField + "() {");
            bw.newLine();
            bw.write("\t\treturn this." + fied.getPropertyName() + ";");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
        }


    }


}
